<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 删除目录及目录下所有文件或删除指定文件
 * @param string $path  待删除目录路径
 * @param bool $delDir  是否删除目录，1或true删除目录，0或false则只删除文件保留目录（包含子目录）
 * @return bool         返回删除状态
 */
function del_dir_and_file($path = '', $delDir = true)
{
    $path   = !empty($path) ? $path : think\facade\Env::get('runtime_path');
    $handle = opendir($path);
    if ($handle) {
        //if (substr(RUNTIME_PATH, -1, 1) == DS) $path = substr($path, 0, -1);
        while (false !== ( $item = readdir($handle) )) {
            if ($item != "." && $item != "..") {
                is_dir($path.DIRECTORY_SEPARATOR.$item) ? del_dir_and_file($path.DIRECTORY_SEPARATOR.$item, $delDir) : unlink($path.DIRECTORY_SEPARATOR.$item);
            }
        }
        closedir($handle);
        if ($delDir) {
            return rmdir($path);
        }
    } else {
        if (file_exists($path)) {
            return unlink($path);
        } else {
            return FALSE;
        }
    }
}

/**
 * 获取后台登录用户id
 * @return mixed
 */
function get_admin_id()
{
    return session('admin_id');
}

/**
 * 获取模板文件
 * @param  string $theme    默认模板主体
 * @param  int $view        当前主体下是否有View文件夹，0没有
 * @return mixed
 */
function get_theme_style($theme = 'default', $view = 0)
{
    if ($view == 0) {
        $template_path = ROOT_PATH . DIRECTORY_SEPARATOR . 'Theme' . DIRECTORY_SEPARATOR . 'content' . DIRECTORY_SEPARATOR;
    } else {
        $template_path = ROOT_PATH . DIRECTORY_SEPARATOR . 'Theme' . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR . 'content' . DIRECTORY_SEPARATOR;
    }
    $file_arr = get_dir_file_name($template_path);

    //循环模板文件把存在配置里的名称更新上
    $styleInfo = array();
    foreach ($file_arr as $key => $value) {
        $temp_style = explode('_', $value);
        if ($temp_style[0] == 'index' || $temp_style[0] == 'index.html') {
            //$styleInfo['index'][$key]['name'] = !empty($temp['TempCon']['index'][$value]) ? $temp['TempCon']['index'][$value] : '';
            $styleInfo['index'][$key]['fileName'] = $value;
        } elseif ($temp_style[0] == 'list' || $temp_style[0] == 'list.html') {
            //$styleInfo['list'][$key]['name'] = !empty($temp['TempCon']['list'][$value]) ? $temp['TempCon']['list'][$value] : '';
            $styleInfo['list'][$key]['fileName'] = $value;
        } elseif ($temp_style[0] == 'show' || $temp_style[0] == 'show.html') {
            //$styleInfo['show'][$key]['name'] = !empty($temp['TempCon']['show'][$value]) ? $temp['TempCon']['show'][$value] : '';
            $styleInfo['show'][$key]['fileName'] = $value;
        } elseif ($temp_style[0] == 'page' || $temp_style[0] == 'page.html') {
            //$styleInfo['page'][$key]['name'] = !empty($temp['TempCon']['page'][$value]) ? $temp['TempCon']['page'][$value] : '';
            $styleInfo['page'][$key]['fileName'] = $value;
        }
    }

    return $styleInfo;
}

/**
 * 获取某个目录下所有的文件名
 * @param  $dir     [目录路径]
 * @return array
 */
function get_dir_file_name($dir)
{
    $array   = array();
    $handler = opendir($dir);
    while ( ($filename = readdir($handler)) !== false ) {
        // 3、目录下都会有两个文件，名字为’.'和‘..’，不要对他们进行操作
        if ($filename != '.' && $filename != '..') {
            // 4、进行处理
            array_push($array,$filename);
        }
    }
    //5、关闭目录
    closedir($handler);
    return $array;
}
